<?php
class ApiAction extends Action {

    public function _initialize()
    {
        header('Access-Control-Allow-Origin: *');
    }
	
    public function getAllCategory()
    {
        $data=get_cache('Category');
        foreach ($data as $key => $value) {
            if($value['model']=="Article")
            {
                $category[]=array('title' =>$value['title'],'id'=>$value['id']);
            }
        }
        $this->ajaxReturn($category,'获取分类成功',1);
    }

    public function getCategoryInfo($id)
    {
        $data=get_cache('Category');
        $category=array('title' =>$data[$id]['title'],'id'=>$data[$id]['id']);
        $this->ajaxReturn($category,'获取分类基本信息成功',1);
    }

    public function getAllArticle($cat)
    {
        $dataModel=M('Article');
        import ('@.ORG.Page');
        if ($cat > 0) {
            $map['category_id']=$cat;
        }
        $map['stauts']=1;
        $count=$dataModel->where($map)->count();
        $page = new Page($count,C ('PAGE_LIMIT'));
        $datas=$dataModel->field('id,title,hit,brief,category_id,add_time')->where($map)->limit($page->firstRow.','.$page->listRows)->order('id DESC')->select();
        if (intval($_GET['page']) > $page->totalPages) {
            $datas = [];
        }
        $this->ajaxReturn($datas,'获取文章列表信息成功',1);
    }

    public function getArticle($id)
    {
        $dataModel=M('Article');
        $data=$dataModel->where('status=1')->find($id);
        $this->ajaxReturn($data,'获取文章详情信息成功',1);
    }

    public function shopping()
    {
        header('Access-Control-Allow-Headers: X-Requested-With');
        $this->ajaxReturn($_POST[0],'提交成功',1);  
    }

    public function pics()
    {
        header('Access-Control-Allow-Headers: X-Requested-With');
        import('@.ORG.Module.MyUploadFile');
        $upload=new MyUploadFile('thumb');
        $upload->run_upload();
        $picInfo=$upload->getUploadFileInfo();
        if(is_array($picInfo))
        {
            foreach ($picInfo as $key => $value) 
            {
                $data[$value['key']]=$value['savename'];
            }
        }
        $this->ajaxReturn($data,'提交成功',1); 

        
    }

    public function getNotice()
    {
        $this->ajaxReturn($_GET['user'],'获取文章详情信息成功',1);
    }

    public function create()
    {
        $this->ajaxReturn($_POST,'提交成功',1); 
    }


}